/******************************************************************************
	This file is a part of the Snapshot Bioinformatics Project

	Copyright (C) 2012-2013 Jarom Schow

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "ELink.h"

#include <QtCore/QUrl>
#include <QtCore/QUrlQuery>
#include <QtCore/QFile>

#include "OutputParser.h"

using namespace Bio;

const QString	c_elinkUrl = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/elink.fcgi";

///////////////////////////////////////////////////////////////////////////////
//	ELink
///////////////////////////////////////////////////////////////////////////////

/**
	@class ELink
	@brief Implementation of the Entrez 'ELink' function.

	Provides an interface to the NCBI Entrez 'ELink' function.

	See http://www.ncbi.nlm.nih.gov/books/NBK25499/ for details on
	the Entrez ELink utility.
**/

/**
	Default Constructor. Creates a new ELink operation.
**/
ELink::ELink() : m_cmd( NoCommand ), m_queryKey( -1 ), m_dateType( NoDate ), m_relDate( -1 )
{
}

/**
**/
ELink::ELink( const QString &db, const QString &dbfrom, Command cmd, const QString &id ) : m_db( db ),
	m_dbfrom( dbfrom ), m_cmd( cmd ), m_queryKey( -1 ), m_dateType( NoDate ), m_relDate( -1 )
{
	m_ids.append( id );
}

/**
**/
ELink::ELink( const QString &db, const QString &dbfrom, Command cmd, const QStringList &ids ) : m_db( db ),
	m_dbfrom( dbfrom ), m_cmd( cmd ), m_ids( ids ), m_queryKey( -1 ), m_dateType( NoDate ), m_relDate( -1 )
{
}

/**
**/
ELink::ELink( const QString &db, const QString &dbfrom, Command cmd, int queryKey, const QString &webEnv ) : m_db( db ),
	m_dbfrom( dbfrom ), m_cmd( cmd ), m_queryKey( queryKey ), m_webEnv( webEnv ), m_dateType( NoDate ), m_relDate( -1 )
{
}

/**
**/
ELink::~ELink()
{
}

/**
**/
bool ELink::exec()
{
	QUrl		url( c_elinkUrl );
	QUrlQuery	urlQuery;

	if( m_dbfrom.isEmpty() || m_db.isEmpty() ){
		error( "No database specified for ELink." );
		return false;
	}

	urlQuery.addQueryItem( "db", m_db );
	urlQuery.addQueryItem( "dbfrom", m_dbfrom );
	
	switch( m_cmd ){
	case Neighbor:
		urlQuery.addQueryItem( "cmd", "neighbor" );
		break;
	case NeighborScore:
		urlQuery.addQueryItem( "cmd", "neighbor_score" );
		break;
	case NeighborHistory:
		urlQuery.addQueryItem( "cmd", "neighbor_history" );
		break;
	case ACheck:
		urlQuery.addQueryItem( "cmd", "acheck" );
		break;
	case NCheck:
		urlQuery.addQueryItem( "cmd", "ncheck" );
		break;
	case LCheck:
		urlQuery.addQueryItem( "cmd", "lcheck" );
		break;
	case LLinks:
		urlQuery.addQueryItem( "cmd", "llinks" );
		break;
	case LLinksLib:
		urlQuery.addQueryItem( "cmd", "llinkslib" );
		break;
	case PRLinks:
		urlQuery.addQueryItem( "cmd", "prlinks" );
		break;
	default:
		break;
	}

	if( !m_linkname.isEmpty() )
		urlQuery.addQueryItem( "linkname", m_linkname );

	if( !m_term.isEmpty() )
		urlQuery.addQueryItem( "term", m_term );

	if( !m_holding.isEmpty() )
		urlQuery.addQueryItem( "holding", m_holding );

	switch( m_dateType ){
	case ModDate:
		urlQuery.addQueryItem( "datetype", "mdat" );
		break;
	case PubDate:
		urlQuery.addQueryItem( "datetype", "pdat" );
		break;
	case EntrezDate:
		urlQuery.addQueryItem( "datetype", "edat" );
		break;
	default:
		break;
	}

	if( m_relDate >= 0 )
		urlQuery.addQueryItem( "reldate", QString::number( m_relDate ) );

	if( m_minDate.isValid() && m_maxDate.isValid() ){
		urlQuery.addQueryItem( "mindate", m_minDate.toString( "yyyy/MM/dd" ) );
		urlQuery.addQueryItem( "maxdate", m_maxDate.toString( "yyyy/MM/dd" ) );
	}

	QFile			output( "ELink.txt" );
	OutputParser	parser( output );

	if( m_queryKey < 0 ){
		if( m_ids.isEmpty() ){
			error( "Must specify IDs or history settings for ELink." );
			return false;
		}

		if( m_ids.count() > 200 )
			return execPost( url, m_ids, parser );
		
		urlQuery.addQueryItem( "id", m_ids.join( "," ) );
	}
	else {
		if( m_webEnv.isEmpty() ){
			error( "Invalid WebEnv specified for ELink." );
			return false;
		}

		urlQuery.addQueryItem( "query_key", QString::number( m_queryKey ) );
		urlQuery.addQueryItem( "WebEnv", m_webEnv );
	}

	url.setQuery( urlQuery );

	return execGet( url, parser );
}

/**
**/
void ELink::setDatabase( const QString &db )
{
	m_db = db;
}

/**
**/
QString ELink::getDatabase() const
{
	return m_db;
}

/**
**/
void ELink::setDatabaseFrom( const QString &db )
{
	m_dbfrom = db;
}

/**
**/
QString ELink::getDatabaseFrom() const
{
	return m_dbfrom;
}

/**
**/
void ELink::addID( const QString &id )
{
	m_ids.append( id );
}

/**
**/
void ELink::setIDs( const QStringList &ids )
{
	m_ids = ids;
}

/**
**/
QStringList ELink::getIDs() const
{
	return m_ids;
}

/**
**/
void ELink::setHistory( int queryKey, const QString &webEnv )
{
	m_queryKey = queryKey;
	m_webEnv = webEnv;
}

/**
**/
int ELink::getQueryKey() const
{
	return m_queryKey;
}

/**
**/
QString ELink::getWebEnv() const
{
	return m_webEnv;
}

/**
**/
void ELink::setLinkname( const QString &linkname )
{
	m_linkname = linkname;
}

/**
**/
QString ELink::getLinkname() const
{
	return m_linkname;
}

/**
**/
void ELink::setTerm( const QString &term )
{
	m_term = term;
}

/**
**/
QString ELink::getTerm() const
{
	return m_term;
}

/**
**/
void ELink::setHolding( const QString &holding )
{
	m_holding = holding;
}

/**
**/
QString ELink::setHolding() const
{
	return m_holding;
}

/**
**/
void ELink::setDateType( DateType type )
{
	m_dateType = type;
}

/**
**/
ELink::DateType ELink::getDateType() const
{
	return m_dateType;
}

/**
**/
void ELink::setRelDate( int num )
{
	m_relDate = num;
}

/**
**/
int ELink::getRelDate() const
{
	return m_relDate;
}

/**
**/
void ELink::setDateRange( const QDate &minDate, const QDate &maxDate )
{
	m_minDate = minDate;
	m_maxDate = maxDate;
}

/**
**/
QDate ELink::getMinDate() const
{
	return m_minDate;
}

/**
**/
QDate ELink::getMaxDate() const
{
	return m_maxDate;
}
